package com.ruoyi.iot.controller;

import com.ruoyi.business.domain.EquipmentVariables;
import com.ruoyi.business.service.IEquipmentVariablesService;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.iot.domain.Coldefinetable;
import com.ruoyi.iot.service.IColdefinetableService;
import com.ruoyi.iot.vo.EquipVariablesVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 点位Controller
 * 
 * @author qiancheng
 * @date 2025-07-15
 */
@RestController
@RequestMapping("/iot/coldefinetable")
public class ColdefinetableController extends BaseController
{
    @Autowired
    private IColdefinetableService coldefinetableService;

    @Autowired
    private IEquipmentVariablesService equipmentVariablesService;

    /**
     * 查询点位列表
     */
    @Anonymous
    @PreAuthorize("@ss.hasPermi('business:coldefinetable:list')")
    @GetMapping("/list")
    public TableDataInfo list(Coldefinetable coldefinetable)
    {
        startPage();
        List<Coldefinetable> list = coldefinetableService.selectColdefinetableList(coldefinetable);
        return getDataTable(list);
    }

    /**
     * 导出点位列表
     */
    @Anonymous
    @Log(title = "点位", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Coldefinetable coldefinetable)
    {
        List<Coldefinetable> list = coldefinetableService.selectColdefinetableList(coldefinetable);
        ExcelUtil<Coldefinetable> util = new ExcelUtil<Coldefinetable>(Coldefinetable.class);
        util.exportExcel(response,list, "点位数据");
    }

    /**
     * 导出设备及点位列表
     */
    @Anonymous
    @PostMapping("/exportVariablesVo")
    public void exportVariablesVo(HttpServletResponse response, Coldefinetable coldefinetable)
    {
        List<EquipVariablesVO> equipVariablesVOS = coldefinetableService.selectVariablesList(new EquipmentVariables());
        ExcelUtil<EquipVariablesVO> util = new ExcelUtil<EquipVariablesVO>(EquipVariablesVO.class);
        util.exportExcel(response,equipVariablesVOS, "设备参数及plc点位数据");
    }

    /**
     * 获取点位详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:coldefinetable:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(coldefinetableService.selectColdefinetableById(id));
    }

    /**
     * 新增点位
     */
    @PreAuthorize("@ss.hasPermi('business:coldefinetable:add')")
    @Log(title = "点位", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Coldefinetable coldefinetable)
    {
        return toAjax(coldefinetableService.insertColdefinetable(coldefinetable));
    }

    /**
     * 修改点位
     */
    @PreAuthorize("@ss.hasPermi('business:coldefinetable:edit')")
    @Log(title = "点位", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Coldefinetable coldefinetable)
    {
        return toAjax(coldefinetableService.updateColdefinetable(coldefinetable));
    }

    /**
     * 删除点位
     */
    @PreAuthorize("@ss.hasPermi('business:coldefinetable:remove')")
    @Log(title = "点位", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(coldefinetableService.deleteColdefinetableByIds(ids));
    }

    /**
     * 导入点位数据
     */
    @Anonymous
    @Log(title = "点位", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<Coldefinetable> util = new ExcelUtil<>(Coldefinetable.class);
        List<Coldefinetable> coldefinetableList = util.importExcel(file.getInputStream());
        String message = coldefinetableService.importColdefinetable(coldefinetableList, Boolean.TRUE);
        return AjaxResult.success(message);
    }


    /**
     * 导入设备列表及点位数据
     */
    @Anonymous
    @PostMapping("/importVariablesVO")
    public AjaxResult importVariablesVO(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<EquipVariablesVO> util = new ExcelUtil<>(EquipVariablesVO.class);
        List<EquipVariablesVO> coldefinetableList = util.importExcel(file.getInputStream());
        String message = coldefinetableService.importVariablesVO(coldefinetableList, Boolean.TRUE);
        return AjaxResult.success(message);
    }


    /**
     * 下载导入模板
     */
    @Anonymous
    //@PreAuthorize("@ss.hasPermi('business:coldefinetable:import')")
    @GetMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<Coldefinetable> util = new ExcelUtil<>(Coldefinetable.class);
        util.importTemplateExcel(response, "点位数据");
    }
}
